<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
if (!Session::has('DSL','admin')){
    die('非法登录');
}
class User extends Controller
{
    public function index(){
        $user_id = Session::get('user_id','admin');
        $keyword = trim(input('get.keyword'));
        if ($keyword == '' || $keyword == false){
            $lists = model('User')->paginate(10);
        }else {
            $lists = db('user')->where('user_name','like','%'.$keyword.'%')->paginate(10);
        }
        $page = $lists->render();
        $this->assign('lists',$lists);
        $this->assign('page',$page);
        return $this->fetch('index');
    }
    
    public function add_admin(){
        $modules_list = db('modules')->paginate(10);
        $page = $modules_list->render();
        $this->assign('modules',$modules_list);
        $this->assign('page',$page);
        return $this->fetch('add');   
    }
    
    public function edit_admin(){
        $user_id = input('get.user_id');
        $user_info = db('user')->where('id',$user_id)->field(['id','user_name','modules_power'])->find();
        $modules_power = $user_info['modules_power'];
        
        $modules_list = db('modules')->paginate(10,false,['query' => request()->param()]);
        $page = $modules_list->render();
        $modules_list = $modules_list->toArray();
        $modules_list = $this->reorganize_modules($modules_list['data'], $modules_power);
        $this->assign('user_info',$user_info);
        $this->assign('modules',$modules_list);
        $this->assign('page',$page);
        return $this->fetch('edit');
    }
    
    protected function reorganize_modules($modules_org,$modules_power)
    {
        if ($modules_power == ''){
            foreach ($modules_org as $k => &$v){
                $v['module_power'] = 'off';
            }
            
            return $modules_org;
        }
        if ($modules_power == 'all'){
            foreach ($modules_org as $k => &$v){
                $v['module_power'] = 'on';
            }
            return $modules_org;
        }
        $modules_power = json_decode($modules_power);
        foreach ($modules_org as $kk => &$vv){
            foreach ($modules_power as $k => $v){
                if ($v == $vv['id']){
                    $vv['module_power'] = 'on';
                    break;
                }else {
                    $vv['module_power'] = 'off';
                }
            }
        }
        return $modules_org;
    }
    
    public function del_admin()
    {
        $result = array('err' => '1','content' => '');
        $user_id = input('post.id');
        $res = db('user')->where('id',$user_id)->delete();
        if ($res == false){
            $result['content'] = '删除失败！';
            return $result;
        }
        $result['content'] = '删除成功！';
        $result['err'] = '0';
        return $result;
    }
    public function save()
    {
        $result = array('err' => '1','content' => '');
        $data = input('post.');
        $rules = [
            'user_name' =>'require|max:20',
            'password' => 'require|max:20'
        ];
        $message = [
            'user_name.require' => '用户名必须！',
            'user_name.max' => '用户名字数请控制在20以内',
            'password.require' => '密码必须',
            'password.max' => '密码字数控制在20以内'
        ];
        $res1 = $this->validate($data, $rules,$message);
        if ($res1 != true){
            $result['content'] = $res1;
            return $result;
        }
        if ($data['password'] == 'notChange'){
            unset($data['password']);
        }else {
            $data['password'] = md5($data['password']);
        }
        $data['modules_power'] = json_encode($data['modules_power']);
        $user_model = model('User');
        $save_user = $user_model->saveAdmin($data);
        if($save_user == false){
            $result['content'] = '用户保存失败！';
            return $result;
        }
        $result['content'] = '保存用户成功！';
        $result['err'] = '0';
        return $result;
    }
     public function user_view()
     {
         return $this->fetch('user_view');
     }
}